import 'package:erp/modules/index/page.dart';
import 'package:erp/modules/mine/page.dart';
import 'package:erp/modules/process_center/page.dart';
import 'package:erp/res/resources.dart';
import 'package:erp/widgets/keep_alive_wrapper.dart';
import 'package:erp/widgets/load_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'controller.dart';

export 'binding.dart';
export 'middleware.dart';

/// 主页
class Main extends StatelessWidget {
  static const double _imageSize = 21.0;

  final List<String> _appBarTitles = [
    '首页',
    '流程申请',
    '我的',
  ];

  Main();

  final List<Widget> _pageList = [
    IndexPage(),
    KeepAliveWrapper(child: FlowCenter()),
    Mine(),
  ];

  List<BottomNavigationBarItem> _buildBottomNavigationBarItem() {
    const _tabImages = [
      [
        LoadAssetImage(
          'main/home',
          width: _imageSize,
          color: Colours.unselected_item_color,
        ),
        LoadAssetImage(
          'main/home_selected',
          width: _imageSize,
          color: Colours.app_main,
        ),
      ],
      [
        LoadAssetImage(
          'main/flow_center',
          width: _imageSize,
          color: Colours.unselected_item_color,
        ),
        LoadAssetImage(
          'main/flow_center_selected',
          width: _imageSize,
          color: Colours.app_main,
        ),
      ],
      [
        LoadAssetImage(
          'main/mine',
          width: _imageSize,
          color: Colours.unselected_item_color,
        ),
        LoadAssetImage(
          'main/mine_selected',
          width: _imageSize,
          color: Colours.app_main,
        ),
      ],
    ];
    return List.generate(_tabImages.length, (i) {
      return BottomNavigationBarItem(
        icon: _tabImages[i][0],
        activeIcon: _tabImages[i][1],
        label: _appBarTitles[i],
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    var controller = Get.find<MainController>();
    return Obx(
      () => Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          backgroundColor: Colors.white,
          items: _buildBottomNavigationBarItem(),
          type: BottomNavigationBarType.fixed,
          currentIndex: controller.currentIndex.value,
          elevation: 5.0,
          iconSize: 21.0,
          selectedFontSize: 20.sp,
          unselectedFontSize: 20.sp,
          unselectedLabelStyle:
              TextStyle(color: Colours.unselected_item_color, fontWeight: FontWeight.w400, fontSize: 20.sp),
          selectedLabelStyle: TextStyle(color: Colours.app_main, fontWeight: FontWeight.w400, fontSize: 20.sp),
          onTap: (index) => {controller.pageController.jumpToPage(index)},
        ),
        // 使用PageView的原因参看 https://zhuanlan.zhihu.com/p/58582876
        body: PageView(
          physics: const NeverScrollableScrollPhysics(), // 禁止滑动
          controller: controller.pageController,
          onPageChanged: (index) => {controller.setCurrentIndex(index)},
          children: _pageList,
        ),
      ),
    );
  }
}
